package d512a.TAKO;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.zxing.client.android.CaptureActivity;

import d512a.TAKO.helpers.DataBaseHelper;
import d512a.TAKO.helpers.DateHelper;

public class ProductInfoMinerals extends Activity {
	private DataBaseHelper foodDB;
	private TextView txtProductName;
	private TextView txtNatriumNaValue;
	private TextView txtKaliumKValue;
	private TextView txtCalciumCaValue;
	private TextView txtMagnesiumMgValue;
	private TextView txtPhosphorPValue;
	private TextView txtJernFeValue;
	private TextView txtKobberCuValue;
	private TextView txtZinkZnValue;
	private TextView txtJodIValue;
	private TextView txtManganMnValue;
	private TextView txtChromCrValue;
	private TextView txtSelenSeValue;
	private TextView txtNikkelNiValue;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_info_minerals);

		handleDatabase();
		addWidgetReferences();
		setWidgetListeners();
		String foodId = getIntent().getExtras().getString("foodId");
		getProductName(foodId);
		getDetailedNutrients(foodId);
        String date = DateHelper.getDate();
		
		if (ProductTabWidgetActivity.fromScan == true) {
            String scanningsID = getIntent().getExtras().getString("scanningsID");
            
            foodDB.insertInRecommended(0, null, scanningsID, 8, date).close();
        } else {
            // Get lastly inserted _id in the Recommended table
            Cursor c = foodDB.getRecommendedPrimKey_id();
            c.moveToFirst();
            String recommend_idPrimKey = c.getString(0);
            c.close();
            foodDB.insertInRecommended(0, recommend_idPrimKey, null, 8, date);
        }
	}

	private void handleDatabase() {
		foodDB = new DataBaseHelper(this);

		try {
			foodDB.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		} finally {
			foodDB.close();
		}
	}

	private void addWidgetReferences() {
		txtProductName = (TextView) findViewById(R.id.txtProductName);
		txtNatriumNaValue = (TextView) findViewById(R.id.txtNatriumNaValue);
		txtKaliumKValue = (TextView) findViewById(R.id.txtKaliumKValue);
		txtCalciumCaValue = (TextView) findViewById(R.id.txtCalciumCaValue);
		txtMagnesiumMgValue = (TextView) findViewById(R.id.txtMagnesiumMgValue);
		txtPhosphorPValue = (TextView) findViewById(R.id.txtPhosphorPValue);
		txtJernFeValue = (TextView) findViewById(R.id.txtJernFeValue);
		txtKobberCuValue = (TextView) findViewById(R.id.txtKobberCuValue);
		txtZinkZnValue = (TextView) findViewById(R.id.txtZinkZnValue);
		txtJodIValue = (TextView) findViewById(R.id.txtJodIValue);
		txtManganMnValue = (TextView) findViewById(R.id.txtManganMnValue);
		txtChromCrValue = (TextView) findViewById(R.id.txtChromCrValue);
		txtSelenSeValue = (TextView) findViewById(R.id.txtSelenSeValue);
		txtNikkelNiValue = (TextView) findViewById(R.id.txtNikkelNiValue);
	}

	private void setWidgetListeners() {

	}

	private void getProductName(String foodId) {
		Cursor cursor = foodDB.getProductNameFromFoodId(foodId);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			txtProductName.setText(cursor.getString(0));
		}
	}

	private void getDetailedNutrients(String foodId) {
		Cursor cursor = foodDB.getMineralsNutrientsFromFoodId(foodId);
		if (cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				int compId = Integer.parseInt(cursor.getString(0));
			    switch(compId) {
    			    case 56:
                        txtNatriumNaValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 57:
                        txtKaliumKValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 58:
                        txtCalciumCaValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 59:
                        txtMagnesiumMgValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 60:
                        txtPhosphorPValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 61:
                        txtJernFeValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 64:
                        txtKobberCuValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 65:
                        txtZinkZnValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 66:
                        txtJodIValue.setText(cursor.getString(1) + " µg");
                        break;
    			    case 67:
                        txtManganMnValue.setText(cursor.getString(1) + " mg");
                        break;
    			    case 68:
                        txtChromCrValue.setText(cursor.getString(1) + " µg");
                        break;
    			    case 69:
                        txtSelenSeValue.setText(cursor.getString(1) + " µg");
                        break;
    			    case 72:
                        txtNikkelNiValue.setText(cursor.getString(1) + " µg");
                        break;
                    default:
                        throw new Error("Fejl 40!");
			    }
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.product_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		Intent i;
		switch (item.getItemId()) {
		case R.id.menu_item_scan:
			startBarcodeScanner();
			return true;
		case R.id.menu_item_home:
			i = new Intent(getApplicationContext(), HomescreenActivity.class);
			startActivity(i);
			return true;
		case R.id.menu_item_shoppingBasket:
			i = new Intent(getApplicationContext(), BasketActivity.class);
			startActivity(i);
			return true;
		case R.id.menu_item_settings:
			i = new Intent(getApplicationContext(), UserInfoActivity.class);
			startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void startBarcodeScanner() {
		Intent i = new Intent(getApplicationContext(), CaptureActivity.class);
		startActivity(i);
	}
}
